/**
 * \file       dolphin.cpp
 * \brief      source codes for build GUI of dolphin media player
 * \version    1.0
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-02
 * \remarks    there is initial Version
 * \bug        unknown
 * \todo       build Manage GUI of dolphin
 *
 * <HR>
 * \version    1.1
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-03
 * \remarks    add some function into this class
 *
 * <HR>
 * \version    1.2
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-04
 * \remarks    add split media file
 *
 * <HR>
 * \version    1.3
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-08
 * \remarks    search media file with directory name and file name
 *
 * <HR>
 * \version    1.4
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-09
 * \remarks    select a file add into play list then play
 *
 * <HR>
 * \version    1.5
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-09
 * \remarks    update OnAddDir and OnAddFile
 *
 * <HR>
 * \version    1.5
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-09
 * \remarks    add play mode choose.
 *
 * <HR>
 * \version    1.6
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-12
 * \remarks    finish loop play mode and random play mode
 *
 * <HR>
 * \version    1.7
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-13
 * \remarks    fixed a bug of set volume
 *
 * <HR>
 * \version    1.8
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-15
 * \remarks    add search file with name in play list. 
 *             that will auto play the target media play 
 *             if find in list
 *
 * <HR>
 * \version    1.9
 * \author     dragon,chinagnu@gmail.com
 * \date       2008-05-15
 * \remarks    add check before load play list data file
 */
#include "xr.h"
#include "dolphin.h"

IMPLEMENT_APP(Dragon)

/** current play mode  */
uint32_t p_mode=0,m_index=0,total=1;

/** create a handle of class server and init it as NULL*/
dolphin *frame = (dolphin *)NULL;

/// init this GUI application 
bool Dragon::OnInit(void)
{
     wxString name = wxString::Format(_T("dolphin~%s"),wxGetUserId().c_str());
     m_checker = new wxSingleInstanceChecker(name);
     if (m_checker->IsAnotherRunning())
     {
		  wxLogError(_("Program already running now. dragon !!"));
		  return false;
     }

     frame = new dolphin(
		  (wxFrame *)NULL, 
		  _T("Dolphin Media Plyaer V1.0"),
		  wxPoint(0,0),
		  wxSize(710,480));

     wxMenu *menu = new wxMenu;
	 menu->Append(101,_T("&Open file"));
     frame->Connect(
		  101,
		  wxEVT_COMMAND_MENU_SELECTED,
		  wxCommandEventHandler(dolphin::OnAddFile));

	 menu->Append(102,_T("&Open URL"));	 

     menu->Append(wxID_EXIT, _T("Quit"));
     frame->Connect( 
		  wxID_EXIT,
		  wxEVT_COMMAND_MENU_SELECTED,
		  wxCommandEventHandler(dolphin::OnQuit));

     wxMenu *menu_tools = new wxMenu;

     wxMenu *menu_set = new wxMenu;

     wxMenu *menu_about = new wxMenu;
     menu_about->Append(wxID_ABOUT, _T("About Me"));
     frame->Connect( 
		  wxID_ABOUT,
		  wxEVT_COMMAND_MENU_SELECTED,
		  wxCommandEventHandler(dolphin::OnAbout));

     wxMenuBar *menubar = new wxMenuBar;
     menubar->Append(menu,_T("General"));
     menubar->Append(menu_tools,_T("Edit"));
     menubar->Append(menu_set,_T("Setting"));
     menubar->Append(menu_about,_T("About"));

     frame->SetMenuBar(menubar);
     frame->CenterOnScreen(wxBOTH); 
     frame->Show(true);

     return true;
}

/// exit this software 
int Dragon::OnExit(void)
{
     delete m_checker;
     return true;
}

/// Create the GUI of dolphin Media Player Version 1.o and display it. 
dolphin::dolphin(wxFrame *parent,const wxString& title,const wxPoint& pos,const wxSize& size)
     :wxFrame(frame,wxID_ANY,title,pos,size,wxCLOSE_BOX|wxSYSTEM_MENU)//wxMINIMIZE_BOX
{
     // inital resource 
     InitXmlResource();
     wxXmlResource::Get()->InitAllHandlers();

     // inital image hander
     wxImage::AddHandler(new wxGIFHandler);
     wxImage::AddHandler(new wxPNGHandler);
     wxBitmap bitmap;

     // set ico for this software
     wxBitmap ico = wxXmlResource::Get()->LoadBitmap(_T("ico")); 
     wxIcon myicon;
     myicon.CopyFromBitmap(ico);
     SetIcon(wxIcon(myicon));

     // load extract resource 
     Init();
     this->Connect( 
		  wxID_ANY,
		  wxEVT_CLOSE_WINDOW,
		  wxCloseEventHandler(dolphin::OnExit),
		  NULL,this);

     // load the xml process modula and media data process modula
     // read data from configure file.
	 class random rmd();
     m_xml = new XMLProcess();
     process = new MediaProcess();

     // set background color
     this->SetBackgroundColour(_T("#94a386"));

     m_timer.SetOwner(this,wxID_ANY);
     this->Connect(
		  wxEVT_TIMER,
		  wxTimerEventHandler(dolphin::OnProgress),
		  NULL,this);

     // build the main menu 
     wxBoxSizer *box = new wxBoxSizer(wxVERTICAL);
     wxBoxSizer *body = new wxBoxSizer(wxHORIZONTAL);
     wxBoxSizer *body_left = new wxBoxSizer(wxVERTICAL);

     // build play area on here
     wxPanel *area = CreateMedaiEngineer(this,465,350);
     body_left->Add(area,0,wxALL,10);

     wxBoxSizer *play_ctrl = new wxBoxSizer(wxHORIZONTAL);
     wxStaticText *play_status = new wxStaticText(
		  this,
		  wxID_ANY,
		  _T("Progress:"),
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxALIGN_LEFT);
     play_ctrl->Add(play_status,0,wxLEFT,10);

     progress = new wxGauge(
		  this,
		  wxID_ANY,
		  100, 
		  wxDefaultPosition,
		  wxSize(280,10),
		  wxGA_SMOOTH|wxGA_HORIZONTAL);
     play_ctrl->Add(progress,0,wxALL,5);

     progress_info = new wxStaticText(
		  this,
		  wxID_ANY,
		  _T(" Stop Status Now"),
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxALIGN_LEFT);
     play_ctrl->Add(progress_info,0,wxALL,0);
     body_left->Add(play_ctrl,0,wxALIGN_CENTER_VERTICAL,10);
     body->Add(body_left,0,wxALL,0);

     wxBoxSizer *body_right = new wxBoxSizer(wxVERTICAL);
	 m_search = new wxSearchCtrl(
		  this,
		  wxID_ANY,
		  wxEmptyString,
		  wxDefaultPosition,
		  wxSize(190,-1),
		  wxTE_PROCESS_ENTER);
	 m_search->SetFocus();
	 m_search->Connect(
		  wxEVT_COMMAND_TEXT_ENTER,
		  wxCommandEventHandler(dolphin::OnSearch),
		  NULL,this);
     body_right->Add(m_search,0,wxALL,10);

     wxPanel *list = CreatePlayList(this,190,305);
     body_right->Add(list,0,wxALL,10);
	 if(m_xml->check_list_file(_T("list.conf")))
	 {
		  LoadPlayList(_T("list.conf"));
	 }

     wxBoxSizer *volum = new wxBoxSizer(wxHORIZONTAL);
     wxStaticText *vol_l = new wxStaticText(
		  this,
		  wxID_ANY,
		  _T("Vol-"),
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxALIGN_LEFT);
     volum->Add(vol_l,0,wxALL,0);

     m_volum= new wxSlider(
		  this, 
		  wxID_ANY, 
		  100, 
		  0, 
		  100,
		  wxDefaultPosition, 
		  wxSize(150,-1),
		  wxSL_HORIZONTAL|wxSL_AUTOTICKS);
     m_volum->Connect(
		  wxEVT_COMMAND_SLIDER_UPDATED,
		  wxCommandEventHandler(dolphin::OnSetVolume),
		  NULL,this);
     volum->Add(m_volum,0,wxALL,0);

     wxStaticText *vol_r = new wxStaticText(
		  this,
		  wxID_ANY,
		  _T("+"),
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxALIGN_RIGHT);
     volum->Add(vol_r,0,wxALL,0);
     body_right->Add(volum,0,wxALIGN_CENTER,10);
     body->Add(body_right,0,wxALL,0);
     box->Add(body,0,wxALL,10);

	 // foot control layout
	 wxBoxSizer *foot = new wxBoxSizer(wxHORIZONTAL);
	 wxBoxSizer *control = new wxBoxSizer(wxHORIZONTAL);
	 control->AddSpacer(15);
     wxCheckBox *m_loop = new wxCheckBox(
		  this,
		  wxID_ANY,
		  _T("loop"),
		  wxDefaultPosition,
		  wxDefaultSize);	
	 loop_id = m_loop->GetId();
	 m_loop->Connect(
		  wxEVT_UPDATE_UI,
		  wxUpdateUIEventHandler(dolphin::OnLoop),
		  NULL,this);
	 m_loop->Connect(
		  wxEVT_COMMAND_CHECKBOX_CLICKED,
		  wxCommandEventHandler(dolphin::OnLoopMode),
		  NULL,this);
	 control->Add(m_loop,0,wxALIGN_CENTER,0);

     wxCheckBox *m_radom = new wxCheckBox(
		  this,
		  wxID_ANY,
		  _T("random"),
		  wxDefaultPosition,
		  wxDefaultSize);
	 radom_id = m_radom->GetId();
	 m_radom->Connect(
		  wxEVT_UPDATE_UI,
		  wxUpdateUIEventHandler(dolphin::OnRadom),
		  NULL,this);
	 m_radom->Connect(
		  wxEVT_COMMAND_CHECKBOX_CLICKED,
		  wxCommandEventHandler(dolphin::OnRadomMode),
		  NULL,this);
	 control->Add(m_radom,0,wxALIGN_CENTER,0);
	 foot->Add(control,0,wxALL,0);
	 
	 foot->AddSpacer(20);
	 // author information begin
     wxBoxSizer *author = new wxBoxSizer(wxVERTICAL);
     wxStaticLine *line = new wxStaticLine(
		  this,
		  wxID_ANY,
		  wxDefaultPosition,
		  wxSize(330,10),
		  wxLI_HORIZONTAL);
     author->Add(line,0,wxALIGN_CENTER,0);
     wxStaticText *author_info = new wxStaticText(
		  this,
		  wxID_ANY,
		  _T(" Copy Right @ 2008-2010 dragon "),
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxALIGN_CENTER);
     author->Add(author_info,0,wxALIGN_CENTER,0);	 
	 foot->Add(author,0,wxALL,0);
	 // author information end
	 foot->AddSpacer(25);

	 wxBoxSizer *tools = new wxBoxSizer(wxHORIZONTAL);
     bitmap = wxXmlResource::Get()->LoadBitmap(_T("folder")); 
     wxBitmapButton *m_adddir = new wxBitmapButton(
		  this,
		  wxID_ANY,
		  bitmap,
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxNO_BORDER);
	 m_adddir->SetBackgroundColour(_T("#5f8e3e"));
     m_adddir->Connect(
		  wxEVT_COMMAND_BUTTON_CLICKED,
		  wxCommandEventHandler(dolphin::OnAddDir),
		  NULL,this);
	 tools->Add(m_adddir,1,wxALIGN_CENTER,0);

     bitmap = wxXmlResource::Get()->LoadBitmap(_T("file")); 
     wxBitmapButton *m_addfile = new wxBitmapButton(
		  this,
		  wxID_ANY,
		  bitmap,
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxNO_BORDER);
	 m_addfile->SetBackgroundColour(_T("#5f8e3e"));
     m_addfile->Connect(
		  wxEVT_COMMAND_BUTTON_CLICKED,
		  wxCommandEventHandler(dolphin::OnAddFile),
		  NULL,this);
	 tools->Add(m_addfile,1,wxALIGN_CENTER,0);

     bitmap = wxXmlResource::Get()->LoadBitmap(_T("pause")); 
     wxBitmapButton *m_pause = new wxBitmapButton(
		  this,
		  wxID_ANY,
		  bitmap,
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxNO_BORDER);
	 m_pause->SetBackgroundColour(_T("#5f8e3e"));
     m_pause->Connect(
		  wxEVT_COMMAND_BUTTON_CLICKED,
		  wxCommandEventHandler(dolphin::OnPause),
		  NULL,this);
	 tools->Add(m_pause,1,wxALIGN_CENTER,0);

     bitmap = wxXmlResource::Get()->LoadBitmap(_T("ctrl")); 
     wxBitmapButton *m_prev = new wxBitmapButton(
		  this,
		  wxID_ANY,
		  bitmap,
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxNO_BORDER);
	 m_prev->SetBackgroundColour(_T("#5f8e3e"));
     m_prev->Connect(
		  wxEVT_COMMAND_BUTTON_CLICKED,
		  wxCommandEventHandler(dolphin::OnPrev),
		  NULL,this);
	 tools->Add(m_prev,1,wxALIGN_CENTER,0);
	
	 bitmap = wxXmlResource::Get()->LoadBitmap(_T("search"));
 	 wxBitmapButton *m_next = new wxBitmapButton(
		  this,
		  wxID_ANY,
		  bitmap,
		  wxDefaultPosition,
		  wxDefaultSize,
		  wxNO_BORDER|wxBU_AUTODRAW);
	 m_next->SetBackgroundColour(_T("#5f8e3e"));
     m_next->Connect(
		  wxEVT_COMMAND_BUTTON_CLICKED,
		  wxCommandEventHandler(dolphin::OnNext),
		  NULL,this);
	 tools->Add(m_next,1,wxALIGN_CENTER,0);
	 foot->Add(tools,0,wxALL,0);
     box->Add(foot,0,wxALL,0);

// 	 m_collpane= new wxCollapsiblePane(this, wxID_ANY, wxEmptyString);
// 	 wxWindow *win = m_collpane->GetPane();
     this->SetSizer(box);

     // Begin listen ...
     m_timer.Start(100);
}

/// destory this GUI and clean evironment. 
dolphin::~dolphin(void)
{
     delete m_Icon;
     delete m_media;
     delete process;
     delete m_xml;
}

void dolphin::Init(void)
{
     wxBitmap roc = wxXmlResource::Get()->LoadBitmap(_T("loop")); 
     wxIcon baricon;
     baricon.CopyFromBitmap(roc);

     m_Icon = new Icon();

     if (!m_Icon->SetIcon(
			  wxIcon(baricon),
			  _T("dolphin media player V1.0\n Author: dragon\n WebSite:http://i-cando.cn/dragon")))
		  wxMessageBox(_T("Could not set icon."));
}

wxPanel* dolphin::CreateMedaiEngineer(wxWindow *parent, int width, int heigh)
{
     wxPanel* panel = new wxPanel(
		  parent,
		  wxID_ANY,
		  wxDefaultPosition,
		  wxSize(width,heigh));
     
     m_media = new wxMediaCtrl(
		  panel,
		  wxID_ANY, 
		  wxEmptyString,
		  wxDefaultPosition,
		  wxSize(width,heigh), 
		  0,
		  wxMEDIABACKEND_GSTREAMER);	
     m_media->Connect(
		  wxEVT_MEDIA_FINISHED,
		  wxMediaEventHandler(dolphin::OnMulitMode),
		  NULL,this);     
     
     return panel;
}

wxPanel* dolphin::CreatePlayList(wxWindow *parent, int width,int heigh)
{
     wxPanel* list = new wxPanel(
		  parent,
		  wxID_ANY,
		  wxDefaultPosition,
		  wxSize(width,heigh));
     list->SetBackgroundColour(_T("#5f8e3e"));
     list->SetAutoLayout(TRUE);
     
     m_list = new wxListCtrl(
		  list, 
		  wxID_ANY, 
		  wxDefaultPosition,
		  wxSize(width,heigh),
		  wxLC_REPORT
		  | wxBORDER_NONE
		  | wxLC_EDIT_LABELS
		  | wxLC_SORT_ASCENDING
		  | wxLC_NO_HEADER
		  | wxLC_HRULES
		  | wxLC_SINGLE_SEL);

     m_list->Connect(
		  wxEVT_COMMAND_LIST_ITEM_SELECTED,
		  wxListEventHandler(dolphin::OnSelect),
		  NULL,this);
     m_list->Connect(
		  wxEVT_COMMAND_LIST_ITEM_ACTIVATED,
		  wxListEventHandler(dolphin::OnProcess),
		  NULL,this);

     m_list->SetTextColour(_T("#008000"));
     m_list->SetBackgroundColour(_T("#e0e0cd"));

     wxListItem info;
     info.m_format = 0;
     info.m_mask = wxLIST_MASK_TEXT|wxLIST_MASK_FORMAT;
     info.SetAlign(wxLIST_FORMAT_LEFT);
     info.SetColumn(0);
     info.SetText(_T("media name list"));
     info.SetWidth(width-15);

     m_list->InsertColumn(0,info);
     
     return list;
}

/**
 *  reload play list from the xml data file
 */
void dolphin::LoadPlayList(wxString conf_file)
{
     c_list = m_xml->get_records(conf_file);
     if (c_list != NULL)
     {
		  m_list->DeleteAllItems();
		  wxString alias;
		  for (uint32_t i=0;i<c_list->GetCount();i++)
		  {
			   alias = m_xml->get_alias(c_list->Item(i));
			   if (alias != wxEmptyString)
			   {
					m_list->InsertItem(i,alias);
			   }
		  }
     }
}

/**
 *  search media files from the choosed directory then 
 *  add into play list data configure file
 */
void dolphin::OnAddDir(wxCommandEvent& WXUNUSED(event))
{
	 wxString homedir = wxString(getenv("HOME"),wxConvUTF8);
     wxDirDialog dialog(
		  this,
		  _T("Add Media Files With Direcotry"),
		  homedir,
		  wxDD_DEFAULT_STYLE|wxDD_NEW_DIR_BUTTON);

     if (dialog.ShowModal() == wxID_OK)
     {
		  wxString dirname = dialog.GetPath();
		  wxDir dir(dirname);
		  if (dir.IsOpened())
		  {
			   wxArrayString *files = new wxArrayString();
			   dir.GetAllFiles(dirname,files,wxEmptyString,wxDIR_DEFAULT);
			   m_xml->add_dir_to_list(_T("list.conf"),c_list,files);
			   delete files;

			   LoadPlayList(_T("list.conf"));
		  }
     }	 
}

/**
 *  Add file to play list and load this file
 */
void dolphin::OnAddFile(wxCommandEvent& WXUNUSED(event))
{
	 wxString homedir = wxString(getenv("HOME"),wxConvUTF8);
	 wxFileDialog filedlg(
		  this,
		  _T("Add Media File To Play List"),
		  homedir,
		  wxEmptyString,
		  _T("All media file(*.*)|*.avi;*.mp3;*.ogg;*.mp4;*.asf;*.mpg;*.wma;*wmv;*.rmvb"),
		  wxFD_OPEN);
	 if (filedlg.ShowModal() == wxID_OK)
     {
		  wxString medianame=filedlg.GetPath();
		  m_xml->add_file_to_list(_T("list.conf"),c_list,medianame);

		  LoadPlayList(_T("list.conf"));
		  if (m_media != NULL)
		  {
			   process->stop(m_media);
			   m_list->SetItemState(c_list->GetCount()-1,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
			   process->play(m_media,c_list->Item(c_list->GetCount()-1));
		  }
	 }
}

/// This function will process the currect selected item of m_listview->m_listctrl.
/// As you see, play the currect item music file and updata the music informaiton 
/// in time.
void dolphin::OnSelect(wxListEvent& event)
{
	 m_index=event.m_itemIndex;
     if (m_media != (wxMediaCtrl *)NULL && c_list != NULL)
     {
		  process->play(m_media,c_list->Item(m_index));
		  m_list->SetItemState(m_index,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
		  GetVolume(m_media,m_volum);
     }
}

/**
 * chang current status between playing and paused
 */
void dolphin::ChangStatus(void)
{
     switch(m_media->GetState())
     {
     case wxMEDIASTATE_PLAYING:
		  process->pause(m_media);
		  break;
     case wxMEDIASTATE_PAUSED:
		  process->play(m_media);
		  break;
     default:
		  break;
     }
}

/**
 * loop play with list
 */
void dolphin::PlayNext(wxMediaCtrl* engineer,wxArrayString* list)
{
     uint32_t i=m_index+total;
     total++;
     if (i>c_list->GetCount()-1)
     {
		  total = 1;
		  m_index = 0;
		  i=0;
     }
	 m_list->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
	 process->play(engineer,list->Item(i));
	 GetVolume(m_media,m_volum);	 
}

/**
 *  random play with list
 */
void dolphin::PlayRandom(wxMediaCtrl* engineer,wxArrayString* list)
{
     uint32_t max=list->GetCount();
	 uint32_t i=rmd.get_random_value(max);

	 m_list->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
	 process->play(engineer,list->Item(i));
	 GetVolume(m_media,m_volum);	 
}

/**
 * search media file with name in play list 
 * return the index of target file in play list if successful.
 * else return -1 if not find anything 
 */
int dolphin::GetIndexOfSearchFile(wxString target)
{
	 if (c_list != NULL)
	 {
		  for(uint32_t i=0;i<c_list->GetCount();i++)
		  {	
			   wxString alias=m_xml->get_alias(c_list->Item(i));
			   if(target.Cmp(alias) == 0)
			   {
					return i;
			   }
		  }
	 }
	 return -1;
}

/**
 * playing or paused current media stream with listctrl event
 */
void dolphin::OnProcess(wxListEvent& event)
{
	 ChangStatus();
}

/**
 * playing or paused current media stream with button clicked event
 */
void dolphin::OnPause(wxCommandEvent& WXUNUSED(event))
{
	 ChangStatus();	 
}

/**
 *  auto choose the play mode
 */
void dolphin::OnMulitMode(wxMediaEvent& WXUNUSED(event))
{
     switch(p_mode)
	 {
	 case NORMAL_MODE:
		  break;
	 case LOOP_MODE:
		  if (c_list != NULL)
		  {
			   PlayNext(m_media,c_list);
		  }
		  break;
	 case RADOM_MODE:
		  if (c_list != NULL)
		  {
			   PlayRandom(m_media,c_list);
		  }
		  break;
	 default:
		  break;
	 }
}

/**
 * Get a signal from Timer and update the progress
 */
void dolphin::OnProgress(wxTimerEvent& event)
{
     process->updata_progress(m_media,progress,progress_info);
}

/**
 * disable random mode when loop mode has been choosed
 */
void dolphin::OnLoop(wxUpdateUIEvent& event)
{
	 wxCheckBox *flag = (wxCheckBox *)FindWindow(radom_id);

	 if (!flag->IsChecked())
		  event.Enable(true);
	 else
		  event.Enable(false);
}

void dolphin::OnLoopMode(wxCommandEvent& event)
{
	 wxCheckBox *flag = (wxCheckBox *)FindWindow(event.GetId());

	 if (flag->IsChecked())
		  p_mode = LOOP_MODE;	
	 else
		  p_mode = NORMAL_MODE;	
}

/**
 * disable loop mode when random mode has been choosed
 */
void dolphin::OnRadom(wxUpdateUIEvent& event)
{
	 wxCheckBox *flag = (wxCheckBox *)FindWindow(loop_id);

	 if (!flag->IsChecked())
		  event.Enable(true);
	 else
		  event.Enable(false);
}

void dolphin::OnRadomMode(wxCommandEvent& event)
{
	 wxCheckBox *flag = (wxCheckBox *)FindWindow(event.GetId());

	 if (flag->IsChecked())
		  p_mode = RADOM_MODE;	
	 else
		  p_mode = NORMAL_MODE;	
}

void dolphin::OnPrev(wxCommandEvent& WXUNUSED(event))
{
	 process->jump_time=20;
	 process->set_media_seek(m_media,1);
}

/**
 * speed control
 */
void dolphin::OnNext(wxCommandEvent& WXUNUSED(event))
{
	 process->jump_time=10;
	 process->set_media_seek(m_media,0);
}

/**
 * play the target that search with name in play list.
 */
void dolphin::OnSearch(wxCommandEvent& WXUNUSED(event))
{
	 wxString result = m_search->GetValue();
	 if (result != wxEmptyString && result != _T(""))
	 {
		  m_search->Clear();
		  if (m_media != (wxMediaCtrl *)NULL)
		  {
			   int target_index=GetIndexOfSearchFile(result);
			   if ( target_index != -1)
			   {
					process->stop(m_media);
					process->play(m_media,c_list->Item(target_index));
					m_list->SetItemState(target_index,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
					GetVolume(m_media,m_volum);
			   }
		  }		
	 }
}

/**
 * Get the value of Volume.
 */
void dolphin::GetVolume(wxMediaCtrl *engineer,wxSlider *volume)
{
     int value = (int)(engineer->GetVolume()*100);
     volume->SetValue(value);
}

/**
 * Set the Value of Volume 
 */
void dolphin::SetVolume(wxMediaCtrl *engineer,wxSlider *volume)
{
	 if(engineer != (wxMediaCtrl*)NULL && engineer->GetState() != wxMEDIASTATE_STOPPED)
	 {
		  if(engineer->SetVolume(volume->GetValue()/100.0) == false )
			   wxMessageBox(wxT("Couldn't set volume!"));
	 }
}

/// This function want to set value of Volume
void dolphin::OnSetVolume(wxCommandEvent& WXUNUSED(event))
{
     SetVolume(m_media,m_volum);
}

// void dolphin::OnFullScreen(wxCommandEvent& event)
// {
//     ShowFullScreen(event.IsChecked());
// }

/// exit this software with window close event 
void dolphin::OnQuit(wxCommandEvent& WXUNUSED(event))
{
     this->Close();
}

/// hide main window as a icon on bar
void dolphin::OnExit(wxCloseEvent& WXUNUSED(event))
{
     this->Show(false);
}

/// show the information about this software, such as author and date and so on. 
void dolphin::OnAbout(wxCommandEvent& WXUNUSED(event))
{
     wxString about =
		  _T("       Dolphin Media Player Version 1.0       ")
		  _T("  \n\n")
		  _T("     Anthor  :  dragon xiao                 \n")
		  _T("     Create  :  May  02th, 2008             \n")
		  _T("     Modify  :  May  15th, 2008             \n")
		  _T("     E-mail  :  chinagnu@gmail.com          \n")
		  _T("     WebSite :  http://i-cando.cn/dragon  \n")
		  _T("     maillist:  http://groups.google.com/group/wxwidget")
		  _T(" \n\n");
	 wxMessageBox(about, _T("About Me"), wxOK|wxICON_INFORMATION, this);
}
